package cn.piesat.controller;

import cn.piesat.util.ResponseWrapperUtil;
import cn.piesat.vo.role.*;
import com.alibaba.fastjson.JSONArray;
import cn.piesat.dto.PiesatRoleInfoDTO;
import cn.piesat.service.intf.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 角色信息访问接口
 *
 * @author xhl
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("add")
    public String add(@Valid @RequestBody AddRoleVO vo) {
        Map<String,String> resultMap = null;
        try {
            resultMap = roleService.add(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseWrapperUtil.executeCustomTips("false","添加角色接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    @PostMapping("update")
    public String update(@Valid @RequestBody UpdateRoleVO vo) {
        Map<String,String> resultMap = null;
        try {
            resultMap = roleService.update(vo);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","修改角色接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    @GetMapping("get/{id}")
    private String getOne(@PathVariable("id") String id) {
        PiesatRoleInfoDTO piesatRoleInfoDTO = roleService.getOne(id);
        return ResponseWrapperUtil.executeResultEntity(piesatRoleInfoDTO);
    }

    @PostMapping("list")
    public String list(@PageableDefault(sort = "createTime",direction = Sort.Direction.DESC)Pageable pageable,
                       @Valid @RequestBody RoleSearchVO searchDTO) {
        Map<String,Object> resultMap = roleService.list(pageable,searchDTO);
        return ResponseWrapperUtil.executeResultMap(resultMap);
    }

    @GetMapping("delete/{id}")
    public String deleteOne(@PathVariable("id") String id) {
        //判断该角色是否挂有菜单
        //判断该角色是否挂有按钮和路由
        //两者都无才能删除，不考虑角色和用户的绑定。要删除角色表数据，角色和用户的绑定关系
        Map<String,String> resultMap = null;
        try{
            resultMap = roleService.deleteOne(id);
        }catch(Exception e){
            e.printStackTrace();
            return ResponseWrapperUtil.executeCustomTips("false","删除角色接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * @Description:角色绑定菜单
     * @Author: zyp
     * @Date: 2021/3/8
     * @param: [authorizeMenuDTO]
     * @return: java.lang.String
     **/
    @PostMapping("/addAuthorizeMenu")
    public String authorizeMenu(@Valid @RequestBody AuthorizeMenuVO authorizeMenuDTO) {
        Map<String,String> resultMap = null;
        try{
            resultMap = roleService.addAuthorizeMenu(authorizeMenuDTO);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseWrapperUtil.executeCustomTips("false","角色绑定菜单接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * @Description:角色绑定资源
     * @Author: zyp
     * @Date: 2021/3/10
     * @param: []
     * @return: java.lang.String
     **/
    @PostMapping("/addAuthorizeResource")
    public String authorizeResource(@Valid @RequestBody AuthorizeResourceVO authorizeResourceVO) {
        Map<String,String> resultMap = null;
        try{
            resultMap = roleService.addAuthorizeResource(authorizeResourceVO);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseWrapperUtil.executeCustomTips("false","角色绑定资源接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }
    /**
     * @Description:根据角色id查询其绑定的菜单
     * @Author: zyp
     * @Date: 2021/3/11
     * @param: [roleId]
     * @return: java.lang.String
     **/
    @GetMapping("/findMenuListByRoleId/{roleId}")
    public String findMenuListByRoleId(@PathVariable("roleId") String roleId){
        JSONArray resultArr =  roleService.findMenuListByRoleId(roleId);
        return ResponseWrapperUtil.executeResultList(resultArr);
    }
    /**
     * @Description:根据角色id查询其绑定的资源
     * @Author: zyp
     * @Date: 2021/3/11
     * @param: [roleId]
     * @return: java.lang.String
     **/
    @GetMapping("/findResourceListByRoleId/{roleId}")
    public String findResourceListByRoleId(@PathVariable("roleId") String roleId){
        JSONArray resultArr =  roleService.findResourceListByRoleId(roleId);
        return ResponseWrapperUtil.executeResultList(resultArr);
    }
    /**
     * @Description:用户管理模块角色下拉框所需数据接口
     * @Author: zyp
     * @Date: 2021/3/15
     * @param: []
     * @return: java.lang.String
     **/
    @GetMapping("/roleList")
    public String roleList(){
        List<PiesatRoleInfoDTO>  resultList= roleService.findAll();
        return ResponseWrapperUtil.executeResultList(resultList);
    }
}
